# Maintainer: Kyle Keen <keenerd@gmail.com>
# Contributor: Dominik Heidler <dheidler@gmail.com>

# it looks like the release model has gotten better
# keeping it more modular for now though

pkgname=bladerf
pkgver=2021.10
_FPGAver=0.14.0
_firmver=2.4.0
_noOScommit=0bba46e  # upstream has pinned a submodule to this
pkgrel=2
pkgdesc="Driver, userspace, fpga & firmware for the bladeRF SDR."
url="https://github.com/Nuand/bladeRF"
# https://github.com/Nuand/bladeRF/tags
arch=('loong64' 'x86_64')
license=('GPL2')
depends=('libusb')
makedepends=('cmake' 'help2man' 'doxygen')
#backup=("etc/bladerf.cfg")

source=(
    "bladerf-$pkgver.tgz::https://github.com/Nuand/bladeRF/archive/refs/tags/$pkgver.tar.gz"
    "noOS-$_noOScommit.tgz::https://github.com/analogdevicesinc/no-OS/archive/$_noOScommit.tar.gz"
    "hostedxA4-$_FPGAver.rbf::https://nuand.com/fpga/v$_FPGAver/hostedxA4.rbf"
    "hostedxA9-$_FPGAver.rbf::https://nuand.com/fpga/v$_FPGAver/hostedxA9.rbf"
    "hostedx40-$_FPGAver.rbf::https://nuand.com/fpga/v$_FPGAver/hostedx40.rbf"
    "hostedx115-$_FPGAver.rbf::https://nuand.com/fpga/v$_FPGAver/hostedx115.rbf"
    "https://www.nuand.com/fx3/bladeRF_fw_v$_firmver.img")

md5sums=('fff2ebe299046c1c73e72c44b0be4314'
         '2c06ff9297d8beb0482a1b0b5e4d3128'
         'c24af70cf56b13123295125a22d5e921'
         '7dfe8c32f260d43a14b9583375a1a6eb'
         '25f402b3734c9aa0d37db9531f93335c'
         '94ee5c7f68abe5c3392202246789fedd'
         '337f570f2e3fdb26abde0f0765196d92')
sha256sums=('77000a42cf5c967aa32e7f8cfa825e21d0298cbeb7c872207ff09f04be718cce'
            'b77a86780e82ff592dd3cbcbf916d6f421f5cb7b523b800255483b1992aea88d'
            '8d45843962c3e930eac3d5cb594ba72b98164c9bbccc218f82975ba05d6f0f5c'
            '622c08b49eec30e420c5ef86521a81c874a27d873df38d3ddc81afc71a461abb'
            'e15fd9b36912cf07fb732e62069b9e519051cb701a811358779000406a2ea93b'
            '603b144ac81ff4ebc254771dd13a379a8d749a5f595232f61d188d2f556e7c02'
            '670d1ca7aa1c6007eb09900351c3aa997e4be316dbc0a2fc1532a908d02cc0a9')

# bladeRF-cli is bundled with libbladeRF
# bladeRF-cli can use libtecla (aur)?

prepare() {
  cd "$srcdir/bladeRF-$pkgver"
  # FS#47168
  sed -i 's|MODE.*$|TAG+="uaccess"|' host/misc/udev/*.rules.in

  # todo, report warning
  sed -i '94i add_definitions(-Wno-error=format-truncation)' host/CMakeLists.txt

  # FS#55326
  sed -i '44s/^else/#&/' host/misc/udev/CMakeLists.txt

  # manually replace the submodules
  # because "git submodule init/update" doesn't work with a release tarball
  rmdir --ignore-fail-on-non-empty thirdparty/analogdevicesinc/no-OS
  ln -s "$srcdir/no-OS-$_noOSver"* thirdparty/analogdevicesinc/no-OS
}

build() {
  cd "$srcdir/bladeRF-$pkgver/host"
  mkdir -p build
  cd build
  CFLAGS+=" -Wno-error=stringop-truncation" cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DINSTALL_UDEV_RULES=ON \
    -DBUILD_DOCUMENTATION=ON \
    -DCMAKE_INSTALL_LIBDIR:PATH=lib \
    -Dusb_LIBRARY:FILEPATH=/usr/lib/libusb-1.0.so \
    -DCMAKE_INSTALL_PREFIX=/usr ../ \
    -DUDEV_RULES_PATH=/usr/lib/udev/rules.d
  make
  # ENABLE_FX3_BUILD needs cypress toolchain
  # really really wants to put files in /usr/lib64
}

package() {
  cd "$srcdir/bladeRF-$pkgver/host/build"
  make DESTDIR="${pkgdir}" install
  cd "$srcdir"
  install -Dm644 "bladeRF_fw_v$_firmver.img" "$pkgdir/usr/share/bladerf/firmware/bladeRF_fw_v$_firmver.img"
  install -Dm644 hostedxA4-$_FPGAver.rbf     "$pkgdir/usr/share/bladerf/fpga/hostedxA4.rbf"
  install -Dm644 hostedxA9-$_FPGAver.rbf     "$pkgdir/usr/share/bladerf/fpga/hostedxA9.rbf"
  install -Dm644 hostedx40-$_FPGAver.rbf     "$pkgdir/usr/share/bladerf/fpga/hostedx40.rbf"
  install -Dm644 hostedx115-$_FPGAver.rbf    "$pkgdir/usr/share/bladerf/fpga/hostedx115.rbf"

  # FS#54105
  cd "$pkgdir/usr/lib/udev/rules.d/"
  mv 88-nuand-bladerf1.rules   70-nuand-bladerf1.rules
  mv 88-nuand-bladerf2.rules   70-nuand-bladerf2.rules
  mv 88-nuand-bootloader.rules 70-nuand-bootloader.rules
}
